﻿<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px; margin:0px; text-align:center}
td{font-size:12px;}
</style>
<?php
include_once 'module/simple_html_dom.php';

/**
 * 数据采集
 */
header('Content-type: text/html; charset=utf-8');

date_default_timezone_set("Asia/Shanghai");

$url = @ $_GET["url"];

if ($url == "") {
	$url = "http://nba.sports.sina.com.cn/players.php";
}else{
	$date = explode("-", $date);
	$year = substr($date[0], -2);
	$date = $year.$date[1];
}

//先清空当前比赛场次的数据
$sql = "delete from live_member_b;";
$sql .= "INSERT INTO live_member_b (memberId, teamId, teamName, memberNo, memberName, memberNameEn) VALUES";

$memberListHTML = file_get_html($url);
$memberListHTML = $memberListHTML->find('table', 1);

$memberId = NULL;
$teamId = NULL;
$teamName = NULL;
$memberNo = NULL;
$memberName = NULL;
$memberNameEn = NULL;
foreach ($memberListHTML->find("tr") as $memberTr) {
	if (count($memberTr->find("td")) == 1) {
		if ($teamName != $memberTr->find("td a", 0)->innertext) {
			$teamName = $memberTr->find("td a", 0)->innertext;
			$teamId = $memberTr->find("td a", 0)->href;
			$teamId = explode("=", $teamId);
			$teamId = $teamId[1];
		} //echo iconv("gb2312", "UTF-8", $memberTr->find("td a", 0)->innertext);
	} else {
		foreach ($memberTr->find("td") as $member) {
			if($member->innertext == ""){
				continue;
			}
			$memberId = $member->find("a", 0)->href;
			$memberId = explode("=", $memberId);
			$memberId = $memberId[1];
			$memberName = $member->find("a", 0)->innertext;
			$memberName = explode(",", $memberName);
			$memberNameEn = $memberName[1];
			$memberName = $memberName[0];
			$memberNo = $member->plaintext;
			$memberNo = explode("&nbsp;", $memberNo);
			$memberNo = $memberNo[0];
			$memberNo = str_replace(iconv("UTF-8", "GBK", "号"), "", $memberNo);
			$sql .= "(" . $memberId . "," . $teamId . ",'" . $teamName . "'," . $memberNo . ",'" . $memberName . "','" . $memberNameEn . "'),";
		}
	}
}

$sql = "SET NAMES GBK;" . substr($sql, 0, strlen($sql) - 1) . ";";

echo iconv("GBK", "UTF-8", $sql);

//连接数据库
$config = require 'conf/config.ini.php'; //加载公用config.ini.php
$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);
//插入比赛比分
mysqli_multi_query($conn, $sql);
?>